package org.jod.anarchytrek.universe;

import java.util.ArrayList;
import java.util.List;

import org.jod.anarchytrek.IMapSystem;
import org.jod.anarchytrek.util.MathUtil;

public class ObjectsSystem implements IMapSystem {

	private int _maxStarNumber;
	private int _currentStarNumber;
	private IMapSystem _system;
	private List _blocked;
	private String[] _tilesType;
		
	

	public ObjectsSystem(int aMaxStarNumber,IMapSystem aSystem , String[] tilesType) {

		_maxStarNumber = aMaxStarNumber;
		_system  = aSystem ;
		_blocked = new ArrayList();
		_tilesType = tilesType;
	}

	public void generate() {
		_system.generate();
		internalGenerate();		
		
	}

	private void internalGenerate() {
		while (_currentStarNumber<_maxStarNumber) {
			
			int rndx = MathUtil.randombetween(1, (_system.width()-1));
			int rndy = MathUtil.randombetween(1, (_system.height()-1));
			if (!_system.blocked(rndx, rndy)) {
				if (MathUtil.randombetween(1, 3) == 1) {
					set(rndx, rndy, _tilesType[tilesType()]);
					_blocked.add(rndx + "-" + rndy);
				}
			}
			_currentStarNumber++;
		}
	}

	private int tilesType() {
		if (_tilesType.length == 1) {
			return 0; 
		}
		return MathUtil.randombetween(0, (_tilesType.length -1));
	}

	public String points() {
		return _system.points();
	}

	public int height() {
		return _system.height();
	}

	public int width() {
		return _system.width();
	}

	public boolean blocked(int x, int y) {
		return _blocked.contains(x + "-" +y);
	}

	public void set(int x, int y, String tilesType) {
		_system.set(x, y, tilesType);
	}

	public int size() {
		return _blocked.size();
	}

}
